﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace VideoArchivePreview
{
    class Functionality
    {
        public static Window getWindows(string name)
        {
            Window wt = null;
            foreach (Window wd in Application.Current.Windows)
            {
                if (wd.Title.Equals(name))
                {
                    wt = wd;
                }
            }
            return wt;
        }

        public static void orderWindows()
        {
            bool widen = false;
            // main.Owner = null;
            
            Window cf = getWindows("Config_frm");
            if (cf != null)
            {
                cf.Owner = null;
               // cf.Topmost = true;
            }
            Window tf = getWindows("TC_frm");
            if (tf != null)
            {
                tf.Owner = null;
               // tf.Topmost = true;
            }
            Window pf = getWindows("Preview_frm");
            if (pf != null)
            {
                pf.Owner = null;
                widen = ((Preview_frm)pf).widen;
               // pf.Topmost = true;
            }
            Window mn = getWindows("MainWindow");
            if (mn != null)
            {
                mn.Owner = null;
              //  mn.Topmost = true;
            }

            //    if (main.Title.Equals("MainWindow"))
            // {
            if ((tf != null) && (pf != null) && (!widen))
            {
                mn.Owner = pf;
                pf.Owner = tf;
                mn.Activate();
             //   mn.Topmost = true;
            }
            else if ((tf != null) && (pf != null) && (widen))
            {
                pf.Owner = mn;
                mn.Owner = tf;
                pf.Activate();
              //  pf.Topmost = true;
            }
            else if (tf != null)
            {
                mn.Owner = tf;
                mn.Activate();
               // mn.Topmost = true;
            }
            else
                if ((cf != null) && (pf != null) && (!widen))
                {
                    mn.Owner = cf;
                    cf.Owner = pf;
                    mn.Activate();
                  //  mn.Topmost = true;
                }
                else if ((cf != null) && (pf != null) && (widen))
                {
                    pf.Owner = mn;
                    mn.Owner = cf;
                    pf.Activate();
                   // pf.Topmost = true;
                }
                else if ((pf != null) && (widen))
                {
                    pf.Owner = mn;
                    pf.Activate();
                   // pf.Topmost = true;
                }
                else if ((pf != null) && (!widen))
                {
                    mn.Owner = pf;
                    mn.Activate();
                   // mn.Topmost = true;
                }
                else if (cf != null)
                {
                    mn.Owner = cf;
                    mn.Activate();
                   // mn.Topmost = true;
                }
        }
    }
}
